package com.message.client;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.message.api.MessageInfo;
import com.message.configuration.MessageConfiguration;
import com.message.enums.DingTalkEnum;
import com.taobao.api.ApiException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

@Data
@Slf4j
public class DingtalkMessageClient implements MessageClient {

    private String client;

    private MessageConfiguration.DingTalkProperties dingTalkProperties;

    @Autowired
    private RedisTemplate redisTemplate;

    private String DING_TALK_ACCESS_TOKEN = "DING_TALK_ACCESS_TOKEN";


    public void initAccessToken() {
        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(dingTalkProperties.getAppKey());
        request.setAppsecret(dingTalkProperties.getAppSecret());
        request.setHttpMethod("GET");
        OapiGettokenResponse response = null;
        try {
            response = client.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        redisTemplate.opsForValue().set(DING_TALK_ACCESS_TOKEN, response.getAccessToken(), (response.getExpiresIn() - 60), TimeUnit.MILLISECONDS);
    }

    public DingTalkClient init() {
        return new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
    }


    @Override
    public void send(MessageInfo messageInfo) {
        DingTalkClient client = init();
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setUseridList(messageInfo.getMessageDestination().getMessageTo());
        request.setAgentId(dingTalkProperties.getAgentId());
        request.setToAllUser(false);
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype(dingTalkProperties.getMsgType());
        if (DingTalkEnum.TEXT.name().equalsIgnoreCase(dingTalkProperties.getMsgType())) {
            msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
//            msg.getText().setContent(dingTalkInfo.getMessageText());
            request.setMsg(msg);
        }
        if (DingTalkEnum.IMAGE.name().equalsIgnoreCase(dingTalkProperties.getMsgType())) {
            msg.setImage(new OapiMessageCorpconversationAsyncsendV2Request.Image());
//            msg.getImage().setMediaId(dingTalkInfo.getMediaId());
            request.setMsg(msg);
        }
        if (DingTalkEnum.FILE.name().equalsIgnoreCase(dingTalkProperties.getMsgType())) {
            msg.setFile(new OapiMessageCorpconversationAsyncsendV2Request.File());
//            msg.getFile().setMediaId(dingTalkInfo.getMediaId());
            request.setMsg(msg);
        }
        if (DingTalkEnum.LINK.name().equalsIgnoreCase(dingTalkProperties.getMsgType())) {
            msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
//            msg.getLink().setTitle(dingTalkInfo.getTitle());
//            msg.getLink().setText(dingTalkInfo.getMessageText());
//            msg.getLink().setMessageUrl(dingTalkInfo.getMessageUrl());
//            msg.getLink().setPicUrl(dingTalkInfo.getPicUrl());
//            request.setMsg(msg);
        }
        if (DingTalkEnum.MARKDOWN.name().equalsIgnoreCase(dingTalkProperties.getMsgType())) {
            msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
//            msg.getMarkdown().setText(dingTalkInfo.getMessageText());
//            msg.getMarkdown().setTitle(dingTalkInfo.getTitle());
//            request.setMsg(msg);
        }
        if (DingTalkEnum.OA.name().equalsIgnoreCase(dingTalkProperties.getMsgType())) {
            msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
            // oa head
            msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
//            // 标题
//            msg.getOa().getHead().setText(dingTalkInfo.getTitle());  //正文
//            msg.getOa().getHead().setBgcolor(dingTalkInfo.getBgcolor()); //样式
//
//            // 链接
//            msg.getOa().setMessageUrl(dingTalkInfo.getMessageUrl());
//            msg.getOa().setPcMessageUrl(dingTalkInfo.getPcMessageUrl());
//
//            // 正文
//            msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
//            msg.getOa().getBody().setContent(dingTalkInfo.getMessageText());
//            msg.getOa().getBody().setTitle(dingTalkInfo.getTitle());
//            // form消息
//            List<OapiMessageCorpconversationAsyncsendV2Request.Form> forms=new ArrayList<>();
//            msg.getOa().getBody().setForm(forms);
//
//            msg.setMsgtype(dingTalkInfo.getDingMsgType());
            request.setMsg(msg);
        }
        if (DingTalkEnum.ACTION_CARD.name().equalsIgnoreCase(dingTalkProperties.getMsgType())) {
            msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
//            msg.getActionCard().setTitle(dingTalkInfo.getTitle());
//            msg.getActionCard().setMarkdown(dingTalkInfo.getMarkDown());
//            msg.getActionCard().setSingleTitle(dingTalkInfo.getTitle());
//            msg.getActionCard().setSingleUrl(dingTalkInfo.getSingleUrl());
//            msg.setMsgtype(dingTalkInfo.getDingMsgType());
            request.setMsg(msg);
        }
        OapiMessageCorpconversationAsyncsendV2Response response = null;
        try {
            String token = (String) redisTemplate.opsForValue().get("");
            response = client.execute(request, token);
            log.info("发送钉钉消息=========================response:{}", response);
        } catch (ApiException e) {
            log.info("发送钉钉消息=========================e:{}", e);

        }

    }


}
